#include "cmdupdatedatatodb.h"
#include "cmdsql.h"
#include "dbctrl.h"

CT_REGISTER_ASYNCCMD(ID_DB_INNER_UPDATEDATA, CCmdUpdateDataToDB);

int CCmdUpdateDataToDB::Do(void* pData)
{
    if(m_pstDB == NULL)
    {
        CT_ERROR(("m_pstDB is null"));
        return Done(RET_SYS_ERROR);
    }

    CCmdSQL* pstCmd = (CCmdSQL*)CT_CMDFACTORY->CreateCmd(ID_DB_SQL);
    if (!pstCmd)
    {
        CT_WARNING((""));
        return Done(RET_SYS_ERROR);
    }

    CT_TRACE(("UPDATE_DATA: update data to db data opType %d datatype %d data key %llu data key2 %s datelen %d",
        m_stReq.m_iOpType,m_stReq.m_stKey.m_chDataType,m_stReq.m_stKey.m_u64ID,
        m_stReq.m_stKey.m_sStrID(),m_stReq.iDataLen));
    //获取数据库更新请求，主要进行sql拼装
    if(GetSqlReq(pstCmd->m_stReq)!=0)
    {
        CT_WARNING(("create mysql error "));
        return Done(RET_SYS_ERROR);
    }

    pstCmd->m_pstMain = m_pstDB;
    RegisterChild(pstCmd);

    CEnpNetHead stHead;
    int iRet = pstCmd->Do(&stHead);
    if (iRet != 0)
    {
        CT_WARNING((""));
        return RET_SYSTEM_FAIL;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetSqlReq(CReqSQL& stReq)
{
    int iRet = 0;
    switch (m_stReq.m_stKey.m_chDataType)
    {
        case DT_PLAYER_BASE_INFO:
            {
				return GetPlayerBaseInfoSqlReq(stReq); 
            }         
           break;
        case DT_PLAYER_DETAIL_INFO:
            {
                return GetPlayerDetailInfoSqlReq(stReq);
            }
            break;
        case DT_PLAYER_DETAIL_EX1_INFO:
            {
                return GetPlayerDetailInfoEx1SqlReq(stReq);
            }
            break;
        case DT_PLAYER_SNS_DETAIL_INFO:
            {
                return GetPlayerSnsDetailInfoSqlReq(stReq);
            }
            break;
        case DT_PLAYER_UNACT_PARTNER:
            {
                return GetUnactPartnerInfoSqlReq(stReq);
            }
            break;
        case DT_ARENA_RANK:
            {
                return GetArenaRankSqlReq(stReq);
            }
            break;
        case DT_ZONE_DATA:
            {
                return GetZoneDataSqlReq(stReq);
            }
            break;
        case DT_CLAN_DATA:
            {
                return GetClanDataSqlReq(stReq);
            }
            break;

        case DT_OSS_PLAYER_DATA:
            return GetOssPlayerDataSqlReq(stReq);

        case DT_OSS_ZONE_DATA:
            return GetOssZoneDataSqlReq(stReq);

        case DT_INSTANCE_ZONE_DETAIL_INFO:
            return GetInstanceZoneDetailInfoSqlReq(stReq);

        case DT_OSS_PLAYER_MONEY:
            return GetOssPlayerMoneySql(stReq);

        default :
            CT_WARNING(("update cmd data type error %d",m_stReq.m_stKey.m_chDataType));
            return -1;
    }
    return iRet;
}

int CCmdUpdateDataToDB::GetPlayerBaseInfoSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("player base info can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CPlayerBaseInfo stPlayerBaseInfo;

    iRet = CDBCtrl::DataDecode(stPlayerBaseInfo, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CPlayerBaseInfo error %d datalen %d point %d",iRet,
            m_stReq.iDataLen,m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CPlayerBaseInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerBaseInfo, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CPlayerBaseInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerBaseInfo, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CPlayerBaseInfoDB::BuildSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetPlayerDetailInfoSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("player detail info can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CPlayerDetailInfo stPlayerDetailInfo;

    iRet = CDBCtrl::DataDecode(stPlayerDetailInfo, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CPlayerDetailInfo error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CPlayerDetailInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerDetailInfo, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CPlayerDetailInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerDetailInfo, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CPlayerDetailInfoDB::BuildSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetPlayerDetailInfoEx1SqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("player detail ex1 info can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CPlayerDetailInfoEx1 stPlayerDetailInfoEx1;
    iRet = CDBCtrl::DataDecode(stPlayerDetailInfoEx1, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CPlayerDetailInfoEx1 error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    unsigned int uiGID = CT_UINT64_LOW(m_stReq.m_stKey.m_u64ID);
    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CPlayerDetailInfoEx1DB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), uiGID, stPlayerDetailInfoEx1, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CPlayerDetailInfoEx1DB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), uiGID, stPlayerDetailInfoEx1, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CPlayerDetailInfoEx1DB::BuildSQL error"));
        return -1;
    }

    return 0;
}


int CCmdUpdateDataToDB::GetPlayerSnsDetailInfoSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("player sns detail info can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CPlayerSnsDetailInfo stPlayerSnsDetailInfo;
    iRet = CDBCtrl::DataDecode(stPlayerSnsDetailInfo, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CPlayerSnsDetailInfo error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CPlayerSnsDetailInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerSnsDetailInfo, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CPlayerSnsDetailInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerSnsDetailInfo, m_pstDB->m_stEncodeBuf);
    }
    
    if(iRet!=0)
    {
        CT_ERROR(("CPlayerDetailInfoDB::BuildSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetUnactPartnerInfoSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("unact partner can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CUnactPartnerInfo stUnactPartnerInfo;
    iRet = CDBCtrl::DataDecode(stUnactPartnerInfo, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CUnactPartnerInfo error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CUnactPartnerInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stUnactPartnerInfo, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CUnactPartnerInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stUnactPartnerInfo, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CUnactPartnerInfoDB::BuildUpdateSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetArenaRankSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("arena rank can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CArenaRank stArenaRank;
    iRet = CDBCtrl::DataDecode(stArenaRank, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CArenaRank error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CArenaRankDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stArenaRank, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CArenaRankDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stArenaRank, m_pstDB->m_stEncodeBuf);
    }
    
    if(iRet!=0)
    {
        CT_ERROR(("CUnactPartnerInfoDB::BuildUpdateSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetZoneDataSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        CT_ERROR(("zone data can not delete, %d", m_stReq.m_iOpType));
        return -1;
    }

    CZoneData stZoneData;
    iRet = CDBCtrl::DataDecode(stZoneData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CZoneData error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_CREATE)
    {
        iRet = CZoneDataDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stZoneData, m_pstDB->m_stEncodeBuf);
    }
    else
    {
        iRet = CZoneDataDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stZoneData, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CZoneDataDB::BuildUpdateSQL error"));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetClanDataSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    CClanData stClanData;
    iRet = CDBCtrl::DataDecode(stClanData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CClanData error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    if (m_stReq.m_iOpType == VOP_DELETE)
    {
        iRet = CClanDataDB::BuildDeleteSQL(stReq, m_pstDB->m_szDBName(), stClanData._uiClanID);
    }
    else
    {
        iRet = CClanDataDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stClanData, m_pstDB->m_stEncodeBuf);
    }

    if(iRet!=0)
    {
        CT_ERROR(("CClanDataDB  error %d", iRet));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetOssPlayerDataSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    CPlayerOssData stPlayerOssData;
    iRet = CDBCtrl::DataDecode(stPlayerOssData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode stPlayerOssData error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    iRet = CPlayerOssDataDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stPlayerOssData, m_pstDB->m_stEncodeBuf);
    if(iRet!=0)
    {
        CT_ERROR(("CPlayerOssData  error %d", iRet));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetOssZoneDataSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    COssZoneData stOssZoneData;
    iRet = CDBCtrl::DataDecode(stOssZoneData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode stOssZoneData error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    iRet = COssZoneDataDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stOssZoneData, m_pstDB->m_stEncodeBuf);
    if(iRet!=0)
    {
        CT_ERROR(("COssZoneData  error %d", iRet));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetInstanceZoneDetailInfoSqlReq(CReqSQL& stReq)
{
    int iRet = 0;

    CInstanceZoneDetailInfo stData;
    iRet = CDBCtrl::DataDecode(stData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CInstanceZoneDetailInfo error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    iRet = CInstanceZoneDetailInfoDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName(), stData, m_pstDB->m_stEncodeBuf);
    if(iRet!=0)
    {
        CT_ERROR(("CInstanceZoneDetailInfo  error %d", iRet));
        return -1;
    }

    return 0;
}

int CCmdUpdateDataToDB::GetOssPlayerMoneySql(CReqSQL& stReq)
{
    CPlayerOssMoney stData;

    int iRet = 0;

    iRet = CDBCtrl::DataDecode(stData, m_stReq.pszData, m_stReq.iDataLen);
    if (iRet != 0)
    {
        CT_ERROR(("DataDecode CCmdUpdateDataToDB error %d datalen %d point %d",iRet,
            m_stReq.iDataLen, m_stReq.pszData));
        return -1;
    }

    iRet = CPlayerOssMoneyDB::BuildReplaceSQL(stReq, m_pstDB->m_szDBName.Get(), stData, m_pstDB->m_stEncodeBuf);
    if(iRet!=0)
    {
        CT_ERROR(("CPlayerOssMoneyDB  error %d", iRet));
        return -1;
    }

    return iRet;
}

int CCmdUpdateDataToDB::CallBack(int iRet)
{
    CT_TRACE(("mp callback"));
    if (iRet)
    {       
        CT_WARNING((""));
        return Done(iRet);
    }

    CT_UPDATE(("-,%d,%d,%d,%s,%d", 
        m_stReq.m_stKey.m_chDataType, CT_UINT64_HIGH(m_stReq.m_stKey.m_u64ID), CT_UINT64_LOW(m_stReq.m_stKey.m_u64ID), 
        m_stReq.m_stKey.m_sStrID(), m_stReq.m_iOpType));

    CT_TRACE(("UPDATE_DATA: db response data opType %d datatype %d data key %llu data key2 %s ",
        m_stReq.m_iOpType,m_stReq.m_stKey.m_chDataType,m_stReq.m_stKey.m_u64ID,m_stReq.m_stKey.m_sStrID()));

    m_pstDB->m_stUpdateMgr.UpdateResponse(m_stReq.m_stKey);
    return Done(iRet);
}


